﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NutFileSynFrm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public async void AppendTextAsync(string str)
        {

            await Task.Run(() =>
            {

                this.Invoke((Action)delegate
                {
                    this.richTextBox1.AppendText(str);
                });

            });

        }

        char[] Hex1 = new char[] { 'A', 'B', 'C', 'D', 'E', 'F' };
        char[] Hex2 = new char[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
        string[] femaleFace;
        string[] maleFace;
        string[] vip;
        public void InitBase()
        {
            string _rs = "";
            _rs = "96,97,98,99";
            foreach(char _c in Hex1)
            {
                _rs += $",9{_c}";
            }
            foreach (char _c in Hex1)
            {
                foreach (char _c2 in Hex2)
                {
                    _rs += $",{_c}{_c2}";
                }
                foreach (char _c2 in Hex1)
                {
                    _rs += $",{_c}{_c2}";
                }

            }

            femaleFace = _rs.Split(',');

            _rs = "";
            foreach (char _c in Hex1)
            {
                _rs += $",0{_c}";
            }
            foreach (char _c in Hex2)
            {
                _rs += $",0{_c}";
            }
            _rs += $",A0,A1,A2";
            vip = _rs.Split(',');

            _rs = "";
            foreach (char _c in Hex2)
            {
                _rs += $",6{_c}";
            }
            foreach (char _c in Hex1)
            {
                _rs += $",6{_c}";
            }
            maleFace = _rs.Split(',');



        }

       /// <summary>
       /// 获取随机非空字节
       /// </summary>
       /// <returns></returns>
        public string RandomContent()
        {
            Random rand = new Random();
            byte[] _buffer = new byte[1];
            rand.NextBytes(_buffer);
            if (String.Format("{0:X2}", _buffer[0]).Equals("00"))
            {
                return RandomContent();
            }
            return String.Format("{0:X2}", _buffer[0]);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type1">1-姓 2-名 3-字</param>
        /// <param name="type2">1-军事 2-内政 3-军师</param>
        /// <returns></returns>
        public string[] GetNames(int type1, int type2)
        {
            string _surname = "";
            string _name = "";
            string _name2 = "";
            Random rand = new Random();

            byte[] _buffer = new byte[2];
            rand.NextBytes(_buffer);



            if (type2 == 1)
            {
                //韩
            }
            else if (type2 == 2)
            {
                //欧阳
                switch (type1)
                {
                    case 1:
                        _surname = "C1,02,BC,DA,B6,A7";
                        return _surname.Split(',');                   
                    default:
                        break;
                }

            }
            else if (type2 == 3)
            {
                //诸葛
            }

            switch (type1)
            {
                
                case 2:
                    _name = $"{RandomContent()},{RandomContent()},{RandomContent()},{RandomContent()}";
                    return _surname.Split(',');
                case 3:
                    _name2 = $"{RandomContent()},{RandomContent()},{RandomContent()},{RandomContent()}";
                    return _surname.Split(',');
                default:
                    break;
            }



            return null;

        }

        public byte[] CreateNewGeneral(byte[] bs,string[] surname,string[] name1,string[] name2,string sex,string type,string vip)
        {
            int  j=0;
            for (int i = 0; i < bs.Length; i++)//逐字节变为16进制字符，以%隔开
            {
                // s += "," + Convert.ToString(bs[i], 16);
                string s = String.Format("{0:X2}", bs[i]);
                if (i / 16 == 68)
                {
                    if (j == 0)
                    {
                        j = i;
                    }
                    this.richTextBox1.AppendText(s + "-");

                }
                else { this.richTextBox1.AppendText(s + " "); }

                if ((i + 1) % 16 == 0)
                {
                    this.richTextBox1.AppendText("\n");
                }


                //if (s.Equals("FF"))
                //{
                //    s = "00";
                //}
                if (i >= 20 && i <= 25)
                {
                    //姓名 20-25
                }

                if (i >= 27 && i <= 30)
                {
                    //名 
                }
                if (i >= 57 && i <= 60)
                {
                    //字
                }

                if (i == 1088)
                {
                    //属性行
                }
                _b = Convert.ToByte(s, 16);


                bs2[i] = _b;


            }
            return null;
        }


        private void Button1_Click(object sender, EventArgs e)
        {
            string input = textBox1.Text;
            char[] values = input.ToCharArray();
            foreach (char letter in values)
            {
                // Get the integral value of the character.
                int value = Convert.ToInt32(letter);
                // Convert the decimal value to a hexadecimal value in string form.
                string hexOutput = String.Format("{0:X}", value);
                AppendTextAsync($"Hexadecimal value of {letter} is {hexOutput}");
            }

        }

        private void Button2_Click(object sender, EventArgs e)
        {
            string hexValues = textBox2.Text;
            string[] hexValuesSplit = hexValues.Split(' ');
            foreach (String hex in hexValuesSplit)
            {
                // Convert the number expressed in base-16 to an integer.
                int value = Convert.ToInt32(hex, 16);
                // Get the character corresponding to the integral value.
                string stringValue = Char.ConvertFromUtf32(value);
                char charValue = (char)value;
                AppendTextAsync($"hexadecimal value = {hex}, int value = {value}, char value = {stringValue} or {charValue}");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string _file = @"C:\temp\1.s13";

            string s = "";

            byte[] bs = File.ReadAllBytes(_file);
            byte[] bs2 = bs.ToArray();
            byte _b;
            int j = 0;
            for (int i = 0; i < bs.Length; i++)//逐字节变为16进制字符，以%隔开
            {
                // s += "," + Convert.ToString(bs[i], 16);
                s = String.Format("{0:X2}", bs[i]);
                if (i / 16 == 68)
                {
                    if (j == 0)
                    {
                        j = i;
                    }
                    this.richTextBox1.AppendText(s + "-");

                }
                else { this.richTextBox1.AppendText(s + " "); }

                if ((i + 1) % 16 == 0)
                {
                    this.richTextBox1.AppendText("\n");
                }


                //if (s.Equals("FF"))
                //{
                //    s = "00";
                //}
                if (i >= 20 && i <= 25)
                {
                    //姓名 20-25
                }

                if (i >= 27 && i <= 30)
                {
                    //名 
                }
                if (i >= 57 && i <= 60)
                {
                    //字
                }

                if (i == 1088)
                {
                    //属性行
                }
                _b = Convert.ToByte(s, 16);


                bs2[i] = _b;


            }
            if (File.Exists(@"c:\temp\2.s13"))
            {

                File.Delete(@"c:\temp\2.s13");
            }
            File.WriteAllBytes(@"c:\temp\2.s13", bs2);

            //s = Convert.ToString(bs,16);
            AppendTextAsync("end" + j);



        }

        private void button4_Click(object sender, EventArgs e)
        {
            Random rand = new Random();
            byte[] _buffer = new byte[1];
            rand.NextBytes(_buffer);
            

            foreach (var _b in _buffer)
            {

                richTextBox1.AppendText(String.Format("{0:X2}", _b) + "\n");
            }


        }
    }
}
